package lance.honeydew.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import lance.honeydew.bean.CategoryBig;
import lance.honeydew.bean.CategoryMedium;
import lance.honeydew.bean.ItemInformation;
import lance.honeydew.bean.ItemStock;
import lance.honeydew.bean.OrderDetail;
import lance.honeydew.bean.OrderInformation;
import lance.honeydew.bean.PaymentMethod;
import lance.honeydew.bean.PublishMessage;
import lance.honeydew.bean.SerialNumber;
import lance.honeydew.bean.ShoppingMoney;
import lance.honeydew.bean.TransportMethod;
import lance.honeydew.bean.UserMember;
import lance.honeydew.dao.CategoryBigDAO;
import lance.honeydew.dao.CategoryMediumDAO;
import lance.honeydew.dao.ItemInformationDAO;
import lance.honeydew.dao.ItemStockDAO;
import lance.honeydew.dao.OrderDetailDAO;
import lance.honeydew.dao.OrderInformationDAO;
import lance.honeydew.dao.PaymentMethodDAO;
import lance.honeydew.dao.PublishMessageDAO;
import lance.honeydew.dao.SerialNumberDAO;
import lance.honeydew.dao.ShoppingMoneyDAO;
import lance.honeydew.dao.TransportMethodDAO;
import lance.honeydew.dao.UserMemberDAO;
import lance.honeydew.util.DateUtil;
import lance.honeydew.util.FileUtil;
import lance.honeydew.util.MessageUtil;
import lance.honeydew.util.SerialNumberUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;

public class ManageService extends BaseService {

	private static final Logger logger = Logger.getLogger(ManageService.class);
	
	/**
	 * checkLoginUserMember
	 * @param email
	 * @param password
	 * @return
	 */
	public UserMember checkLoginUserMember(String email, String password) {
		
		UserMember user = null;
		try {
			
			user = userMemberDao.checkLoginUserMember(email, password);
			
		}catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return user;
	}
	
	/*會員管理*/
	private UserMemberDAO userMemberDao;

	public UserMemberDAO getUserMemberDao() {
		return userMemberDao;
	}

	public void setUserMemberDao(UserMemberDAO userMemberDao) {
		this.userMemberDao = userMemberDao;
	}
	
	/*Manage Service*/
	/**
	 * findUserMemberByUserId
	 * @param userId
	 * @return
	 */
	public UserMember findUserMemberByUserId(Long userId) {
		
		UserMember result = null;
		
		try {
			
			UserMember obj = new UserMember();
			obj.setUserid(userId);
			result = userMemberDao.findUserMemberByUserId(obj);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}	
	
	/**
	 * queryUserMemberByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<UserMember> queryUserMemberByQb(UserMember qb, int pageNum, int pageSize) {
		
		List<UserMember> result = null;
		
		try {
			
			result = userMemberDao.queryUserMemberByQb(qb, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * createUserMemberByObj
	 * @param obj
	 * @return
	 */
	public boolean createUserMemberByObj(UserMember obj) {
		
		boolean result = false;
		
		try {
			
			if (StringUtils.isNotEmpty(obj.getBrithdayStr())) {
				obj.setBrithday(DateUtil.dbStrToDate(obj.getBrithdayStr() + " 00:00:00"));
			}
			
			userMemberDao.create(obj);			
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateUserMemberByObj
	 * @param obj
	 * @return
	 */
	public boolean updateUserMemberByObj(UserMember obj) {
		
		boolean result = false;
		
		try {
			
			if (StringUtils.isNotEmpty(obj.getBrithdayStr())) {
				obj.setBrithday(DateUtil.dbStrToDate(obj.getBrithdayStr() + " 00:00:00"));
			}
			
			userMemberDao.update(obj);			
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * deleteUserMemberByUserId
	 * @param userId
	 * @return
	 */
	public boolean deleteUserMemberByUserId(Long userId) {
		
		boolean result = false;
		
		try {
			
			userMemberDao.deleteUserMemberByUserId(userId);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/*公告管理*/
	private PublishMessageDAO publishMessageDAO;	
	
	
	public PublishMessageDAO getPublishMessageDAO() {
		return publishMessageDAO;
	}

	public void setPublishMessageDAO(PublishMessageDAO publishMessageDAO) {
		this.publishMessageDAO = publishMessageDAO;
	}

	/**
	 * queryPublishMessageByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<PublishMessage> queryPublishMessageByQb(PublishMessage qb, int pageNum, int pageSize) {
		
		List<PublishMessage> result = null;
		
		try {
			
			result = publishMessageDAO.queryPublishMessageByQb(qb, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * createPublishMessageByObj
	 * @param obj
	 * @return
	 */
	public boolean createPublishMessageByObj(PublishMessage obj) {
		
		boolean result = false;
		
		try {
			
			publishMessageDAO.create(obj);
			result = true;
			
		} catch (Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * 
	 * @param seqId
	 * @return
	 */
	public PublishMessage findPublishMessageBySeqId(Long seqId) {
		
		PublishMessage result = null;
		
		try {
			
			result = publishMessageDAO.findPublishMessageBySeqId(seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updatePublishMessageByObj
	 * @param obj
	 * @return
	 */
	public boolean updatePublishMessageByObj(PublishMessage obj) {
		
		boolean result = false;
		
		try {
			
			publishMessageDAO.update(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * deletePublishMessageBySeqId
	 * @param seqId
	 * @return
	 */
	public boolean deletePublishMessageBySeqId(Long seqId) {
		
		boolean result = false;
		
		try {
			
			PublishMessage obj = new PublishMessage(seqId);
			publishMessageDAO.delete(PublishMessage.PROP_SEQID, obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/*付款方式管理*/
	private PaymentMethodDAO paymentMethodDAO;

	public PaymentMethodDAO getPaymentMethodDAO() {
		return paymentMethodDAO;
	}

	public void setPaymentMethodDAO(PaymentMethodDAO paymentMethodDAO) {
		this.paymentMethodDAO = paymentMethodDAO;
	}
	
	/**
	 * queryPaymentMethodByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<PaymentMethod> queryPaymentMethodByQb(PaymentMethod qb, int pageNum, int pageSize) {
		
		List<PaymentMethod> list = null;
		
		try {
			
			list = paymentMethodDAO.queryPaymentMethodByQb(qb, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return list;
	}
	
	/**
	 * createPaymentMethodByObj
	 * @param obj
	 * @return
	 */
	public boolean createPaymentMethodByObj(PaymentMethod obj) {
		
		boolean result = false;
		
		try {
			
			paymentMethodDAO.create(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return false;
	}
	
	/**
	 * findPaymentMethodBySeqId
	 * @param seqId
	 * @return
	 */
	public PaymentMethod findPaymentMethodBySeqId(Long seqId) {
		
		PaymentMethod result = null;
		
		try {
			
			result = paymentMethodDAO.findPaymentMethodBySeqId(seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updatePaymentMethodByObj
	 * @param obj
	 * @return
	 */
	public boolean updatePaymentMethodByObj(PaymentMethod obj) {
		
		boolean result = false;
		
		try {
			
			paymentMethodDAO.update(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return false;
	}
	
	/**
	 * deletePaymentMethodBySeqId
	 * @param seqId
	 * @return
	 */
	public boolean deletePaymentMethodBySeqId(Long seqId) {
		
		boolean result = false;
		
		try {
			
			PaymentMethod obj = new PaymentMethod(seqId);
			paymentMethodDAO.delete(PaymentMethod.PROP_SEQID, obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return false;
	}
	
	/*運送方式管理*/
	private TransportMethodDAO transportMethodDAO;

	public TransportMethodDAO getTransportMethodDAO() {
		return transportMethodDAO;
	}

	public void setTransportMethodDAO(TransportMethodDAO transportMethodDAO) {
		this.transportMethodDAO = transportMethodDAO;
	}
	
	/**
	 * queryTransportMethodByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<TransportMethod> queryTransportMethodByQb(TransportMethod qb, int pageNum, int pageSize) {
		
		List<TransportMethod> list = null;
		
		try {
			
			list = transportMethodDAO.queryTransportMethodByQb(qb, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return list;
	}
	
	/**
	 * createTransportMethodByObj
	 * @param obj
	 * @return
	 */
	public boolean createTransportMethodByObj(TransportMethod obj) {
		
		boolean result = false;
		
		try {
			
			transportMethodDAO.create(obj);
			
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return false;
	}
	
	/**
	 * findTransportMethodBySeqId
	 * @param seqId
	 * @return
	 */
	public TransportMethod findTransportMethodBySeqId(Long seqId) {
		
		TransportMethod result = null;
		
		try {
			
			result = transportMethodDAO.findTransportMethodBySeqId(seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateTransportMethodByObj
	 * @param obj
	 * @return
	 */
	public boolean updateTransportMethodByObj(TransportMethod obj) {
		
		boolean result = false;
		
		try {
			
			transportMethodDAO.update(obj);
			
			result = true;
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return false;
	}
	
	/**
	 * deleteTransportMethodBySeqId
	 * @param seqId
	 * @return
	 */
	public boolean deleteTransportMethodBySeqId(Long seqId) {
		
		boolean result = false;
		
		try {
			
			TransportMethod obj = new TransportMethod(seqId);
			transportMethodDAO.delete(TransportMethod.PROP_SEQID, obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return false;
	}
	
	/*商品大類管理*/
	private CategoryBigDAO categoryBigDAO;

	public CategoryBigDAO getCategoryBigDAO() {
		return categoryBigDAO;
	}

	public void setCategoryBigDAO(CategoryBigDAO categoryBigDAO) {
		this.categoryBigDAO = categoryBigDAO;
	}
	
	/**
	 * queryCategoryBigDAOByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<CategoryBig> queryCategoryBigByQb(CategoryBig qb, int pageNum, int pageSize) {
		
		List<CategoryBig> list = null;
		
		try {
			
			list = categoryBigDAO.queryCategoryBigByQb(qb, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return list;
	}
	
	/**
	 * createCategoryBigByObj
	 * @param obj
	 * @return
	 */
	public boolean createCategoryBigByObj(CategoryBig obj) {
		
		boolean result = false;
		
		try {
			
			categoryBigDAO.create(obj);
			
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return false;
	}
	
	/**
	 * findCategoryBigBySeqId
	 * @param seqId
	 * @return
	 */
	public CategoryBig findCategoryBigBySeqId(Long seqId) {
		
		CategoryBig result = null;
		
		try {
			
			result = categoryBigDAO.findCategoryBigBySeqId(seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateCategoryBigByObj
	 * @param obj
	 * @return
	 */
	public boolean updateCategoryBigByObj(CategoryBig obj) {
		
		boolean result = false;
		
		try {
			
			categoryBigDAO.update(obj);
			
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return false;
	}
	
	/**
	 * deleteCategoryBigBySeqId
	 * @param seqId
	 * @return
	 */
	public boolean deleteCategoryBigBySeqId(Long seqId) {
		
		boolean result = false;
		
		try {
			
			CategoryBig obj = new CategoryBig(seqId);
			categoryBigDAO.delete(CategoryBig.PROP_SEQID, obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return false;
	}
	
	/*商品中類管理*/
	private CategoryMediumDAO categoryMediumDAO;

	public CategoryMediumDAO getCategoryMediumDAO() {
		return categoryMediumDAO;
	}

	public void setCategoryMediumDAO(CategoryMediumDAO categoryMediumDAO) {
		this.categoryMediumDAO = categoryMediumDAO;
	}
	
	/**
	 * findAllCategoryBigList
	 * @return
	 */
	public List<CategoryBig> findAllCategoryBigList() {

		List<CategoryBig> list = null;
		
		try {
			
			list = categoryBigDAO.findAllCategoryBigList();
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return list;		
	}
	
	/**
	 * queryCategoryMediumByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<CategoryMedium> queryCategoryMediumByQb(CategoryBig qb, int pageNum, int pageSize) {
		
		List<CategoryMedium> list = null;
		
		try {
			
			list = categoryBigDAO.queryCategoryMediumByQb(qb, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return list;
	}
	
	/**
	 * createCategoryMediumByObj
	 * @param obj
	 * @return
	 */
	public boolean createCategoryMediumByObj(CategoryMedium obj) {
		
		boolean result = false;
		
		try {
			
			categoryMediumDAO.create(obj);
			
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findCategoryMediumBySeqId
	 * @param seqId
	 * @return
	 */
	public CategoryMedium findCategoryMediumBySeqId(Long seqId) {
		
		CategoryMedium result = null;
		
		try {
			
			result = categoryMediumDAO.findCategoryMediumBySeqId(seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateCategoryMediumByObj
	 * @param obj
	 * @return
	 */
	public boolean updateCategoryMediumByObj(CategoryMedium obj) {
		
		boolean result = false;
		
		try {
			
			categoryMediumDAO.update(obj);
			
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return false;
	}
	
	/**
	 * deleteCategoryMediumBySeqId
	 * @param seqId
	 * @return
	 */
	public boolean deleteCategoryMediumBySeqId(Long seqId) {
		
		boolean result = false;
		
		try {
			
			CategoryMedium obj = new CategoryMedium(seqId);
			categoryMediumDAO.delete(CategoryMedium.PROP_SEQID, obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/*會員購物金管理*/
	private ShoppingMoneyDAO shoppingMoneyDAO;

	public ShoppingMoneyDAO getShoppingMoneyDAO() {
		return shoppingMoneyDAO;
	}

	public void setShoppingMoneyDAO(ShoppingMoneyDAO shoppingMoneyDAO) {
		this.shoppingMoneyDAO = shoppingMoneyDAO;
	}
	
	/**
	 * queryShoppingMoneyByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<ShoppingMoney> queryShoppingMoneyByQb(UserMember qb, int pageNum, int pageSize) {
		
		List<ShoppingMoney> result = null;
		
		try {
			
			result = userMemberDao.queryShoppingMoneyByQb(qb, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * createShoppingMoneyByObj
	 * @param obj
	 * @return
	 */
	public boolean createShoppingMoneyByObj(ShoppingMoney obj) {
		
		boolean result = false;
		
		try {
			
			if (StringUtils.isNotEmpty(obj.getProcessdateStr())) {
				obj.setProcessdate(DateUtil.dbStrToDate(obj.getProcessdateStr() + " 00:00:00"));
			}
			shoppingMoneyDAO.create(obj);
			
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findShoppingMoneyBySeqId
	 * @param seqId
	 * @return
	 */
	public ShoppingMoney findShoppingMoneyBySeqId(Long seqId) {
		
		ShoppingMoney result = null;
		
		try {
			
			result = shoppingMoneyDAO.findShoppingMoneyBySeqId(seqId);
			if (result.getProcessdate() != null) {
				result.setProcessdateStr(DateUtil.dbDateToStr(result.getProcessdate()));
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateShoppingMoneyByObj
	 * @param obj
	 * @return
	 */
	public boolean updateShoppingMoneyByObj(ShoppingMoney obj) {
		
		boolean result = false;
		
		try {
			
			if (StringUtils.isNotEmpty(obj.getProcessdateStr())) {
				obj.setProcessdate(DateUtil.dbStrToDate(obj.getProcessdateStr() + " 00:00:00"));
			}
			shoppingMoneyDAO.update(obj);
			result = true;
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * deleteShoppingMoneyBySeqId
	 * @param seqId
	 * @return
	 */
	public boolean deleteShoppingMoneyBySeqId(Long seqId) {
		
		boolean result = false;
		
		try {
			
			ShoppingMoney obj = new ShoppingMoney(seqId);
			shoppingMoneyDAO.delete(ShoppingMoney.PROP_SEQID, obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findUserMemberByEmailAndName
	 * @param bo
	 * @return
	 */
	public UserMember findUserMemberByEmailAndName(UserMember bo) {
		
		UserMember result = null;
		
		try {
			
			result = userMemberDao.findUserMemberByEmailAndName(bo);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/*商品資訊管理*/
	private ItemInformationDAO itemInformationDAO;
	private SerialNumberDAO serialNumberDAO;

	
	public SerialNumberDAO getSerialNumberDAO() {
		return serialNumberDAO;
	}

	public void setSerialNumberDAO(SerialNumberDAO serialNumberDAO) {
		this.serialNumberDAO = serialNumberDAO;
	}

	public ItemInformationDAO getItemInformationDAO() {
		return itemInformationDAO;
	}

	public void setItemInformationDAO(ItemInformationDAO itemInformationDAO) {
		this.itemInformationDAO = itemInformationDAO;
	}
	
	/**
	 * queryItemInformationByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<ItemInformation> queryItemInformationByQb(ItemInformation qb, int pageNum, int pageSize) {
		
		List<ItemInformation> result = null;
		
		try {
			
			result = itemInformationDAO.queryItemInformationByQb(qb, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findSerialNumberByTableName
	 * @param tableName
	 * @return
	 */
	public String generateSerialNumber(String tableName) {
		
		String result = null;
		Date today = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			
			SerialNumber serialNumber = serialNumberDAO.findSerialNumberByTableName(tableName);
			
			//如果不是今天，將流水號重置
			if (sdf.format(today)
					.compareTo(sdf.format(serialNumber.getUpdateTime())) != 0) {
				
				serialNumber.setSerialnumber(new Long(1));
				serialNumber.setUpdateTime(today);
				serialNumberDAO.update(serialNumber);
			}
			
			
			result = SerialNumberUtil.generateSerialNumber(today, String.valueOf(serialNumber.getSerialnumber()), 5);
			
			//取得後+1更新資料
			serialNumber.setSerialnumber(serialNumber.getSerialnumber() + 1);
			serialNumberDAO.update(serialNumber);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * createItemInformationByObj
	 * @param obj
	 * @return
	 */
	public boolean createItemInformationByObj(ItemInformation obj) {
		
		boolean result = false;
		
		try {
			
			if (obj != null && StringUtils.isNotEmpty(obj.getNumber())) {
				
				//主要圖片
				if (obj.getTmpMainphotoFile() != null) {
					String mainPath = "MainPhoto.jpg";
					FileUtil.saveFile(obj.getTmpMainphotoFile(), mainPath, 
							MessageUtil.getString("honeydew.fileupload.location") + obj.getNumber());
					obj.setMainphoto(mainPath);
				}
				
				//次要圖片
				if (obj.getTmpSecondaryphotoFile() != null) {
					for (int i = 0; i < obj.getTmpSecondaryphotoFile().size(); i++) {
						String secondaryPath = "SecondaryPhoto" + String.valueOf(i + 1) + ".jpg";
						FileUtil.saveFile(obj.getTmpSecondaryphotoFile().get(i), secondaryPath, 
								MessageUtil.getString("honeydew.fileupload.location") + obj.getNumber());
						
						if (i == 0 && StringUtils.isNotEmpty(obj.getSecondaryphoto())) {
							obj.setSecondaryphoto("");
						}
						
						String tmpStr = "" + obj.getSecondaryphoto();
						obj.setSecondaryphoto(tmpStr + secondaryPath + ",");
					}
				}
				
				//尺吋圖片
				if (obj.getTmpSizephotoFile() != null) {
					String sizePath = "SizePhoto.jpg";
					FileUtil.saveFile(obj.getTmpSizephotoFile(), sizePath, 
							MessageUtil.getString("honeydew.fileupload.location") + obj.getNumber());
					obj.setSizephoto(sizePath);
				}
			}
			
			if (StringUtils.isNotEmpty(obj.getStartdateStr())) {
				obj.setStartdate(DateUtil.dbStrToDate(obj.getStartdateStr() + " 00:00:00"));
			}
			
			if(StringUtils.isNotEmpty(obj.getEnddateStr())) {
				obj.setEnddate(DateUtil.dbStrToDate(obj.getEnddateStr() + " 00:00:00"));
			}
			
			serialNumberDAO.create(obj);
			result = true;
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findCategoryMediumListByBigSeqId
	 * @param bigSeqId
	 * @return
	 */
	public List<CategoryMedium> findCategoryMediumListByBigSeqId(Long bigSeqId) {
		
		List<CategoryMedium> result = null;
		
		try {
			
			result = categoryMediumDAO.findCategoryMediumListByBigSeqId(bigSeqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findItemInformationBySeqId
	 * @param seqId
	 * @return
	 */
	public ItemInformation findItemInformationBySeqId(Long seqId) {
		
		ItemInformation result = null;
		
		try {
			
			result = itemInformationDAO.findItemInformationBySeqId(seqId);
			
			if (result != null) {
				result.setStartdateStr(DateUtil.dbDateToStr(result.getStartdate()));
				result.setEnddateStr(DateUtil.dbDateToStr(result.getEnddate()));
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateItemInformationByObj
	 * @param obj
	 * @return
	 */
	public boolean updateItemInformationByObj(ItemInformation obj) {
		
		boolean result = false;
		
		try {
			
			if (obj != null && StringUtils.isNotEmpty(obj.getNumber())) {
				
				//主要圖片
				if (obj.getTmpMainphotoFile() != null) {
					String mainPath = "MainPhoto.jpg";
					FileUtil.saveFile(obj.getTmpMainphotoFile(), mainPath, 
							MessageUtil.getString("honeydew.fileupload.location") + obj.getNumber());
					obj.setMainphoto(mainPath);
				}
				
				//次要圖片
				if (obj.getTmpSecondaryphotoFile() != null) {
					for (int i = 0; i < obj.getTmpSecondaryphotoFile().size(); i++) {
						String secondaryPath = "SecondaryPhoto" + String.valueOf(i + 1) + ".jpg";
						FileUtil.saveFile(obj.getTmpSecondaryphotoFile().get(i), secondaryPath, 
								MessageUtil.getString("honeydew.fileupload.location") + obj.getNumber());
						
						if (i == 0 && StringUtils.isNotEmpty(obj.getSecondaryphoto())) {
							obj.setSecondaryphoto("");
						}
						
						String tmpStr = "" + obj.getSecondaryphoto();
						obj.setSecondaryphoto(tmpStr + secondaryPath + ",");
					}
				}
				
				//尺吋圖片
				if (obj.getTmpSizephotoFile() != null) {
					String sizePath = "SizePhoto.jpg";
					FileUtil.saveFile(obj.getTmpSizephotoFile(), sizePath, 
							MessageUtil.getString("honeydew.fileupload.location") + obj.getNumber());
					obj.setSizephoto(sizePath);
				}
			}
			
			if (StringUtils.isNotEmpty(obj.getStartdateStr())) {
				obj.setStartdate(DateUtil.dbStrToDate(obj.getStartdateStr() + " 00:00:00"));
			}
			
			if(StringUtils.isNotEmpty(obj.getEnddateStr())) {
				obj.setEnddate(DateUtil.dbStrToDate(obj.getEnddateStr() + " 00:00:00"));
			}
			
			itemInformationDAO.update(obj);
			result = true;
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * deleteItemInformationBySeqId
	 * @param seqId
	 * @return
	 */
	public boolean deleteItemInformationBySeqId(Long seqId) {
		
		boolean result = false;
		
		try {
			
			ItemInformation obj = new ItemInformation(seqId);
			itemInformationDAO.delete(ItemInformation.PROP_SEQID, obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/*商品庫存管理*/
	private ItemStockDAO itemStockDAO;

	public ItemStockDAO getItemStockDAO() {
		return itemStockDAO;
	}

	public void setItemStockDAO(ItemStockDAO itemStockDAO) {
		this.itemStockDAO = itemStockDAO;
	}
	
	/**
	 * queryItemStockByItemInformationOfNumber
	 * @param number
	 * @return
	 */
	public List<ItemStock> queryItemStockByItemInformationOfNumber(String number) {
		
		List<ItemStock> result = null;
		
		try {
			
			result = itemInformationDAO.queryItemStockByItemInformationOfNumber(number);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findItemInformationByNumber
	 * @param number
	 * @return
	 */
	public ItemInformation findItemInformationByNumber(String number) {
		
		ItemInformation result = null;
		
		try {
			
			result = itemInformationDAO.findItemInformationByNumber(number);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * createItemStockByObj
	 * @param obj
	 * @return
	 */
	public boolean createItemStockByObj(ItemStock obj) {
		
		boolean result = false;
		
		try {
			
			if (StringUtils.isNotEmpty(obj.getStartdateStr())) {
				obj.setStartdate(DateUtil.dbStrToDate(obj.getStartdateStr() + " 00:00:00"));
			}
			
			if(StringUtils.isNotEmpty(obj.getEnddateStr())) {
				obj.setEnddate(DateUtil.dbStrToDate(obj.getEnddateStr() + " 00:00:00"));
			}
			
			itemStockDAO.create(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findItemStockBySeqId
	 * @param seqId
	 * @return
	 */
	public ItemStock findItemStockBySeqId(Long seqId) {
		
		ItemStock result = null;
		
		try {
			
			result = itemStockDAO.findItemStockBySeqId(seqId);
			
			if (result != null) {
				result.setStartdateStr(DateUtil.dbDateToStr(result.getStartdate()));
				result.setEnddateStr(DateUtil.dbDateToStr(result.getEnddate()));
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateItemStockByObj
	 * @param obj
	 * @return
	 */
	public boolean updateItemStockByObj(ItemStock obj) {
		
		boolean result = false;
		
		try {
			
			if (StringUtils.isNotEmpty(obj.getStartdateStr())) {
				obj.setStartdate(DateUtil.dbStrToDate(obj.getStartdateStr() + " 00:00:00"));
			}
			
			if(StringUtils.isNotEmpty(obj.getEnddateStr())) {
				obj.setEnddate(DateUtil.dbStrToDate(obj.getEnddateStr() + " 00:00:00"));
			}
			
			itemStockDAO.update(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * deleteItemStockBySeqId
	 * @param seqId
	 * @return
	 */
	public boolean deleteItemStockBySeqId(Long seqId) {
		
		boolean result = false;
		
		try {
			
			ItemStock obj = new ItemStock(seqId);
			itemStockDAO.delete(ItemStock.PROP_SEQID, obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/*訂單資訊管理*/
	private OrderInformationDAO orderInformationDAO;

	public OrderInformationDAO getOrderInformationDAO() {
		return orderInformationDAO;
	}

	public void setOrderInformationDAO(OrderInformationDAO orderInformationDAO) {
		this.orderInformationDAO = orderInformationDAO;
	}
	
	/**
	 * queryOrderInformationByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<OrderInformation> queryOrderInformationByQb(OrderInformation qb, int pageNum, int pageSize) {
		
		List<OrderInformation> result = null;
		
		try {
			
			result = orderInformationDAO.queryOrderInformationByQb(qb, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findAllPaymentMethodList
	 * @return
	 */
	public List<PaymentMethod> findAllPaymentMethodList() {
		
		List<PaymentMethod> result = null;
		
		try {
			
			result = paymentMethodDAO.findAllPaymentMethodList();
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findAllTransportMethodList
	 * @return
	 */
	public List<TransportMethod> findAllTransportMethodList() {
		
		List<TransportMethod> result = null;
		
		try {
			
			result = transportMethodDAO.findAllTransportMethodList();
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findUserMemberByEmail
	 * @param email
	 * @return
	 */
	public UserMember findUserMemberByEmail(String email) {
		
		UserMember result = null;
		try {
			
			result = userMemberDao.findUserMemberByEmail(email);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * createOrderInformationByObj
	 * @param obj
	 * @return
	 */
	public boolean createOrderInformationByObj(OrderInformation obj) {
		
		boolean result = false;
		
		try {
			
			if (StringUtils.isNotEmpty(obj.getProcessfinaldateStr())) {
				obj.setProcessfinaldate(DateUtil.dbStrToDate(obj.getProcessfinaldateStr() + " 00:00:00"));
			}
			
			if(StringUtils.isNotEmpty(obj.getOutorderdateStr())) {
				obj.setOutorderdate(DateUtil.dbStrToDate(obj.getOutorderdateStr() + " 00:00:00"));
			}
			
			orderInformationDAO.create(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findOrderInformationBySeqId
	 * @param seqId
	 * @return
	 */
	public OrderInformation findOrderInformationBySeqId(Long seqId) {
		
		OrderInformation result = null;
		try {
			
			result = orderInformationDAO.findOrderInformationBySeqId(seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateOrderInformationByObj
	 * @param obj
	 * @return
	 */
	public boolean updateOrderInformationByObj(OrderInformation obj) {
		
		boolean result = false;
		try {
			
			if (StringUtils.isNotEmpty(obj.getProcessfinaldateStr())) {
				obj.setProcessfinaldate(DateUtil.dbStrToDate(obj.getProcessfinaldateStr() + " 00:00:00"));
			}
			
			if(StringUtils.isNotEmpty(obj.getOutorderdateStr())) {
				obj.setOutorderdate(DateUtil.dbStrToDate(obj.getOutorderdateStr() + " 00:00:00"));
			}
			
			orderInformationDAO.update(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		return result;
	}
	
	/**
	 * deleteOrderInformationBySeqId
	 * @param seqId
	 * @return
	 */
	public boolean deleteOrderInformationBySeqId(Long seqId) {
		
		boolean result = false;
		
		try {
			
			OrderInformation obj = new OrderInformation(seqId);
			orderInformationDAO.delete(OrderInformation.PROP_SEQID, obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/*訂單明細管理*/
	private OrderDetailDAO orderDetailDAO;
	
	public OrderDetailDAO getOrderDetailDAO() {
		return orderDetailDAO;
	}

	public void setOrderDetailDAO(OrderDetailDAO orderDetailDAO) {
		this.orderDetailDAO = orderDetailDAO;
	}

	/**
	 * queryOrderDetailByQb
	 * @param qb
	 * @return
	 */
	public List<OrderDetail> queryOrderDetailByQb(OrderInformation qb, int pageNum, int pageSize) {
		
		List<OrderDetail> result = null;
		try {
			
			result = orderInformationDAO.queryOrderDetailByQb(qb, pageNum, pageSize);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		return result;
	}
	
	/**
	 * findOrderInformationByNumber
	 * @param Number
	 * @return
	 */
	public OrderInformation findOrderInformationByNumber(String Number) {
		
		OrderInformation result = null;
		try {
			
			result = orderInformationDAO.findOrderInformationByNumber(Number);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		return result;
		
	}
	
	/**
	 * createOrderDetailByObj
	 * @param obj
	 * @return
	 */
	public boolean createOrderDetailByObj(OrderDetail obj) {
		
		boolean result = false;
		try {
			
			orderDetailDAO.create(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		return result;
		
	}
	
	/**
	 * findOrderDetailBySeqId
	 * @param seqId
	 * @return
	 */
	public OrderDetail findOrderDetailBySeqId(Long seqId) {
		
		OrderDetail result = null;
		try{
			
			result = orderDetailDAO.findOrderDetailBySeqId(seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		return result;
		
	}
	
	/**
	 * @param seqId
	 * @return
	 */
	public boolean deleteOrderDetailBySeqId(Long seqId) {
		
		boolean result = false;
		
		try {
			
			OrderDetail obj = new OrderDetail(seqId);
			orderDetailDAO.delete(OrderDetail.PROP_SEQID, obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * updateOrderDetailByObj
	 * @param obj
	 * @return
	 */
	public boolean updateOrderDetailByObj(OrderDetail obj) {
		
		boolean result = false;
		
		try {
			orderDetailDAO.update(obj);
			result = true;
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
}
